﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace TestPing
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// 参照 https://www.cnblogs.com/chengulv/p/4520562.html
    /// </summary>
    public partial class MainWindow : Window
    {
        private PingResult bean = new PingResult();
        private Ping pingSender = new Ping();//构造Ping实例            
        private PingOptions options = new PingOptions//Ping 选项设置
        {
            DontFragment = true
        };
        private byte[] buffer = Encoding.ASCII.GetBytes("");//测试数据        
        private int timeout = 120;//设置超时时间

        DispatcherTimer dispatcherTimer = new DispatcherTimer();//创建定时器

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = bean;

            //定时器
            dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                this.RunPing(PingTarget.PUSH);
                this.RunPing(PingTarget.DNS);
            }).Start();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button but = (Button)sender;
            if (but.Content.ToString() == "启动")
            {
                dispatcherTimer.Start();
                but.Content = "停止";
            }
            else
            {
                dispatcherTimer.Stop();
                but.Content = "启动";
            }
        }

        private void RunPing(PingTarget target)
        {
            this.Dispatcher.Invoke(
            new Action(() =>
            {
                string ipStr = "";//远程服务器IP 
                if (target == PingTarget.PUSH)
                {
                    ipStr = this.PushDomain.Text.Trim();
                }
                else if (target == PingTarget.DNS)
                {
                    ipStr = this.DnsDomain.Text.Trim();
                }

                if (ipStr == "")
                {
                    return;
                }

                //调用同步 send 方法发送数据,将返回结果保存至PingReply实例
                // PingReply reply = pingSender.Send(ipStr, timeout, buffer, options); 这个方式对114.114.114.114不通
                PingReply reply = pingSender.Send(ipStr);
                if (target == PingTarget.PUSH)
                {
                    bean.AddPushResult(reply);
                }
                else if (target == PingTarget.DNS)
                {
                    bean.AddDnsResult(reply);
                }
            }));
        }

    }
}
